﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CSP.Common.Tools;
using CSP.Common.Web;
using CSP.Bll;
using System.Collections.ObjectModel;

namespace CSP.Web.UserControls
{
    public partial class AppointmentRecord : CSPUserControlBase
    {
        private const string ORDERNUMBERSKEY = "ordernumbers";
        private List<string> OrderNumbers
        {
            get
            {
                return ViewState[ORDERNUMBERSKEY] as List<string>;

            }
            set
            {
                ViewState[ORDERNUMBERSKEY] = value;
            }
        }

        private int _serviceId;

        public int ServiceId
        {
            get
            {
                return _serviceId;
            }
            set
            {
                _serviceId = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();
            }
        }

        private void InitPage()
        {

            OrderNumbers = FindOrderNumbersByServiceInfoId(_serviceId);
            SetPagerParm(0, 15, OrderNumbers.Count);
            ICollection<Model.CSP.Order> orders = GetByOrderNumbers(OrderNumbers, this.U_AppointmentRecordPager.PageIndex, this.U_AppointmentRecordPager.PageSize);
            BindAppointmentRecordUI(orders);
        }

        /// <summary>
        /// 根据serviceInfoId获取其订单Id
        /// </summary>
        /// <param name="orderNumber"></param>
        /// <returns></returns>
        private List<string> FindOrderNumbersByServiceInfoId(int serviceInfoId)
        {
            List<string> result = new List<string>();
            using (var orderBll = CSPBllFactory.GetInstance().GetOrderBll())
            {
                var ids = orderBll.FindOrderNumbersByServiceInfoId(serviceInfoId);
                if (ids.Count != 0)
                {
                    result = ids;
                }
            }
            return result;
        }

        /// <summary>
        /// 根据多个Ordernumbers分页 获取多个订单
        /// </summary>
        /// <param name="orderNumbers"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        private ICollection<Model.CSP.Order> GetByOrderNumbers(List<string> orderNumbers, int pageIndex, int pageSize)
        {
            ICollection<Model.CSP.Order> result = new Model.CSP.Order[0];
            using (var orderBll = CSPBllFactory.GetInstance().GetOrderBll())
            {
                result = orderBll.GetByOrderNumbers(orderNumbers, pageIndex, pageSize);
            }

            return result;
        }

        /// <summary>
        /// 绑定预约记录
        /// </summary>
        /// <param name="orders"></param>
        private void BindAppointmentRecordUI(ICollection<Model.CSP.Order> orders)
        {
            this.R_AppointmentRecord.DataSource = orders;
            this.R_AppointmentRecord.DataBind();
        }


        /// <summary>
        /// 设置分页参数
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageCount"></param>
        private void SetPagerParm(int pageIndex, int pageSize, int pageCount)
        {
            this.U_AppointmentRecordPager.PageSize = pageSize;
            this.U_AppointmentRecordPager.PageIndex = pageIndex;
            this.U_AppointmentRecordPager.ItemCount = pageCount;
        }



        /// <summary>
        /// 字符串部分加密从头开始
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        protected string PartEncrypt(string str)
        {
            return CommonTool.PartEncrypt(str);
        }

        protected void U_AppointmentRecordPager_IndexChange(object sender, EventArgs e)
        {
            ICollection<Model.CSP.Order> orders = GetByOrderNumbers(OrderNumbers, this.U_AppointmentRecordPager.PageIndex, this.U_AppointmentRecordPager.PageSize);
            BindAppointmentRecordUI(orders);
        }


    }
}